import requests
from lxml import etree
from lxml import etree
import time
import pymysql
# conn =pymysql.connect(host='127.0.0.1',port=3306,user='root',password='12345678',db='house',charset='utf8')
# cursor=conn.cursor()
#headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}
headers = {
    'authority': 'list.jd.com',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cache-control': 'max-age=0',
    # Requests sorts cookies= alphabetically
    # 'cookie': 'shshshfpa=d3d84cf0-c673-397c-d8fc-47b04ed97b74-1593346607; unpl=JF8EAJdnNSttUEsGUExWHxdAG1tVW1laS0dQb28HVl1YGAQGTFAeFxl7XlVdXhRKEx9uZBRXX1NKUw4fAisSEXteU11bD00VB2xXVgQFDQ8WUUtBSUt-SV9XWFUOThcGb28BZG1bS2QFGjIYEBZKVFBcWgx7JwRfVzVTXFBOVAArAysTIAkJCFtYDk8XASJkB1JcUU9WAh8yGiIT; shshshfpb=sS5P%2FSyz488lI%2BsycWNILGw%3D%3D; __jdu=237085365; areaId=19; ipLoc-djd=19-1655-0-0; pinId=oV4EwF0cRZSB_GaZZI2wSQ; pin=jd_LfVDtlKlzBct; unick=jd_LfVDtlKlzBct; _tp=T%2FBS1E%2BqHDd5DRfGe%2BtXCg%3D%3D; _pst=jd_LfVDtlKlzBct; user-key=75cf510d-621f-4373-9adb-d75da2393184; cn=0; __jdv=122270672|baidu-search|t_262767352_baidusearch|cpc|332797414195_0_91cdfe54aa7045c1ab193311ba2fc448|1651508397498; PCSYCityID=CN_440000_441900_0; __jdc=122270672; __jda=122270672.237085365.1651504011.1651665297.1651669989.3; shshshfp=629dc896c782cda64890bcee4ed70bd8; wlfstk_smdl=8may7pom8q9ruh3ygp6by9gakgred5sm; TrackID=1ZKpRioMz6ohYVXzs1nf9Kj0SglBCehOw4Jrmx9lPmmu_Htwk9EasnuPVqLtH_bzwS4NWvkPTPT131wl_vMyl2T-XFOV1yBT3QYsoyCTPBsY; thor=91832CF8DC4EA64BF93076E607ADF2557309D5050F9AD4A8525F80F8ADFBC12AAA285F74A03D27B0032AB6D8BD8BC7865E99E9988F518315279CCDD44ED4D7414C5D73E170EFF4A02887D095F8CEAEBDD42BC3415820869E4CA96FCA137BA82DE36F901E33367538140141AB691E1FD14A794AD75DFE6A5C16D06B8CDC4111A4F876479AFCE8D673CFBDE1E0D95A5435AD2299240995ACF6C1C6FE4E6DCC8DE6; ceshi3.com=000; token=c9b7b37af4f9c86d2cd3a9a0914dd7db,3,917594; shshshsID=3695209d97d69c90bbb42ccae658eaa5_17_1651670711647; __jdb=122270672.66.237085365|3.1651669989; ip_cityCode=1655; 3AB9D23F7A4B3C9B=M3TGGRNOAL5PZ6DKWZHJHCLG4D3HGHI4SOZWGCWPPFMNF4OPYUWP5KNXYPTL4J2BNHJBDKSYKEB5FQQ25VV4P54VRU',
    'referer': 'https://passport.jd.com/',
    'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'document',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-site': 'same-site',
    'sec-fetch-user': '?1',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
}

conn =pymysql.connect(host='127.0.0.1',autocommit=True,port=3306,user='root',password='hwj99999',db='profile',charset='utf8')
cursor=conn.cursor()
# sql='insert into sources values("对比Excel，轻松学习Python数据分析 数据分析师实操工具书，对比Excel学习Python代码 低门槛高效率 自动化办公 Python编程从入门到实践","//product.dangdang.com/26512595.html","Python虽然是一门编程语言，但是在数据分析领域实现的功能和Excel的基本功能一样，而Excel又是大家比较熟悉、容易上手的软件，可以通过Excel数据分析去对比学习Python数据分析。本书将数据分析过程中涉及的每一个操作都对照讲解，让你从熟悉的Excel操作中去学习对应的Python实现，而不是直接学习Python代码，大大降低了学习门槛，消除了大家对代码的恐惧心理。这也是本书的一大特色，让读者可以像学Excel数据分析一样，轻松学习Python数据分析。","admin",0)'
# cursor.execute(sql)
for i in range(1, 50):#aaaaa
    num = str(i)
    url = "https://category.dangdang.com/pg"+num+"-cp01.54.06.00.00.00.html"
    res1 = requests.get(url, headers=headers)
    selector = etree.HTML(res1.text)
    for j in range(1,61):
        link=selector.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li['+str(j)+']/p[1]/a/@href')
        title = selector.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li['+str(j)+']/p[1]/a/text()')
        jianjie = selector.xpath('/html/body/div[2]/div/div[3]/div[1]/div[1]/div[2]/div/ul/li['+str(j)+']/p[2]/text()')
        if(len(link)>0 and len(title)>0 and len(jianjie)>0):
            if(len(str(jianjie[0]))<300):
                sql = "insert into sources values("
                sql += "'"
                sql += str(title[0])
                sql += "',"

                sql+="'"
                sql+=str(link[0])
                sql+="',"

                sql += "'"
                sql += str(jianjie[0])
                sql += "',"
                sql+='\'admin\',0)'
                try:
                    cursor.execute(sql)
                except:
                    pass
                print(sql)


    # / html / body / div[2] / div / div[3] / div[1] / div[1] / div[2] / div / ul / li[60] / p[1]
    # // *[ @ id = "J_goodsList"] / ul / li[44] / div / div[3] / a
    # print(str(info))
    # // *[ @ id = "J_goodsList"] / ul / li[60] / div / div[3] / a




